package com.pureut.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.domain.dto.SysTheDropDto;
import com.pureut.system.domain.SysOfCode;

import com.pureut.system.domain.SysOfInfo;
import com.pureut.system.mapper.SysOfCodeMapper;
import com.pureut.system.mapper.SysOfInfoMapper;
import com.pureut.system.service.ISysOfCodeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Service
public class SysOfCodeServiceImpl implements ISysOfCodeService {

    @Resource
    private SysOfCodeMapper ofCodeMapper;

    @Resource
    private SysOfInfoMapper ofInfoMapper;

    @Override
    public List<SysOfCode> selectOfCodeList(SysOfCode ofcode) {
        return ofCodeMapper.selectOfCodeList(ofcode);
    }

    /**
     * 编码新增
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int ofCodeAdd(SysOfCode ofCode) throws Exception {
        ofCode.setCreateBy(SecurityUtils.getUsername());
        String kl = ofCode.getOp();
        int is = ofCodeMapper.selectOneOfCode(ofCode.getOfCodeStr());
        if(is>0){
            throw new SQLException("配置编码不能重复,请重新输入!");
        }
        List<SysOfInfo> list= JSONArray.parseArray(kl, SysOfInfo.class);
        int ids =  ofCodeMapper.ofCodeAdd(ofCode); //新增主表 返回主键ID
        int rows1 = 0;
        long idx = ofCode.getOfCodeId();
        if(ids>0){
            for (SysOfInfo sy :
            list) {
                sy.setCodeId(idx);
                rows1 = ofInfoMapper.ofCodeInfoAdd(sy);//更新从表
                if(rows1==0){
                    throw new SQLException("抛出异常");
                }
            }
        }
        return rows1;
    }

    /**
     * 删除编码
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteOfCodeById(String ofCodeId) throws Exception {
        String[] idS = ofCodeId.split(",");
        int ui = ofCodeMapper.deleteOfCodeById(idS);
        int o=0;
        if(ui>0){
            o = ofInfoMapper.deleteInfoById(idS);
           if(o==0){
               throw new SQLException("抛出异常");
           }
        }
        return o;
    }

    /**
     * 修改编码
     *
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateOfEdit(SysOfCode ofCode) throws Exception {
        String kl = ofCode.getOp();
        List<SysOfInfo> list= JSONArray.parseArray(kl, SysOfInfo.class);
        int ui = ofCodeMapper.updateOfEdit(ofCode);
        long kp = ofCode.getOfCodeId();
        int rows2=0;
        if(ui>0){
            int c= ofInfoMapper.deleteInfoByIdS(kp);
            if(c>0){
                for (SysOfInfo sy :
                        list) {
                    sy.setCodeId(kp);
                    rows2 = ofInfoMapper.ofCodeInfoAdd(sy);//更新从表
                    if(rows2==0){
                        throw new Exception("抛出异常");
                    }
                }
            }else{
                throw new Exception("抛出异常");
            }
        }
        return rows2;
    }

    /**
     * 查询单个编码
     *
     * @return
     */
    @Override
    public SysOfCode selectofCodeById(Long ofCodeId) {
        SysOfCode sysOfCodeS = new SysOfCode();
        sysOfCodeS.setOfCodeId(ofCodeId);
        List<SysOfCode> list = ofCodeMapper.selectOfCodeList(sysOfCodeS);
        sysOfCodeS = list.get(0);
        List<SysOfInfo> list2 = ofInfoMapper.selectOfInfoList(ofCodeId);
        sysOfCodeS.setInList(list2);
        return sysOfCodeS;
    }

    @Override
    public List<SysTheDropDto> selectOfCodeListThe() {
        return ofCodeMapper.selectOfCodeListThe();
    }
}
